package com.learn.ssm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.learn.ssm.mapper.ScheduleMapper;
import com.learn.ssm.pojo.Schedule;
import com.learn.ssm.service.api.ScheduleService;
import com.learn.ssm.utils.PageBean;
import com.learn.ssm.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ScheduleServiceImpl implements ScheduleService {

    @Autowired
    private ScheduleMapper scheduleMapper;


    @Override
    public R page(Integer pageSize, Integer currentPage) {

        // 开启分页功能
        PageHelper.startPage(currentPage, pageSize);

        // 查询每页的数据
        List<Schedule> scheduleList = scheduleMapper.queryList(pageSize, currentPage);

        // 封装 PageInfo
        PageInfo<Schedule> schedulePageInfo = new PageInfo<>(scheduleList);

        // 封装 PageBean
        PageBean<Schedule> pageBean = new PageBean<>(currentPage, pageSize, schedulePageInfo.getTotal(), scheduleList);

        // 返回 R
        return R.ok(pageBean);
    }

    @Override
    public R remove(Integer scheduleId) {

        int rows = scheduleMapper.deleteScheduleById(scheduleId);
        if(rows > 0) {
            return R.ok(null);
        }
        return R.fail(null);
    }

    @Override
    public R save(Schedule schedule) {
        int rows = scheduleMapper.insert(schedule);
        if(rows > 0) {
            return R.ok("保存成功");
        }
        return R.fail("保存失败");
    }

    @Override
    public R update(Schedule schedule) {
        if(schedule.getId() == null){
            return R.fail("id不能为空");
        }
        int rows = scheduleMapper.update(schedule);
        if(rows > 0) {
            return R.ok("修改成功");
        }
        return R.fail("修改失败");
    }
}
